package com.scalar.chiptrack.adaptor.ikanos.stats;

import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTML;
import javax.swing.text.html.parser.DTD;
import javax.swing.text.html.parser.DocumentParser;
import javax.swing.text.MutableAttributeSet;
import java.io.Reader;

/**
 * User: ChandraSekhar
 * Date: Jul 7, 2008
 * Time: 3:51:45 PM
 */
public class STATSHtmlParser extends HTMLEditorKit.ParserCallback
{

    private String m_token;
	private boolean m_isInput;

	public STATSHtmlParser()
    {
	}

	public void parse(Reader reader) throws Exception{

		DTD dtd = DTD.getDTD("HTML");
		DocumentParser documentParser = new DocumentParser (dtd);
        documentParser.parse (reader, this, true /*ignore CharSet*/);

	}

	public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos){

		if(t == HTML.Tag.INPUT){

			//System.out.println();

			HTML.Attribute name = HTML.getAttributeKey("name");
			HTML.Attribute value = HTML.getAttributeKey("value");
            
			if( "site2pstoretoken".equalsIgnoreCase((String)a.getAttribute(name))){
				setToken((String)a.getAttribute(value));
			}

			m_isInput = true;

		}else{
			m_isInput = false;
		}

	}

	public void handleText (char[] data, int pos) {
		String text = new String(data);
	}

	public void setToken(String token){
		m_token = token;
	}

	public String getToken(){
		return m_token;
	}
}
